﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace MvcUtil
{
    public class ObjectUtil
    {
        /// <summary>
        /// Generates the number.
        ///  </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objName">Name of the obj.</param>
        /// <param name="query">The query.</param>
        /// <param name="clause">The clause.</param>
        /// <returns>
        /// Number auto generate
        /// </returns>
        public static string GenerateNumber<T>(string objName, IQueryable<T> query, Expression<Func<T, int>> clause)
        {
            int id = 0;
            string numberAuto = string.Empty;

            if (query.Count()>0)
            {
                if (query.Max(clause) > 0)
                {
                    id = query.Max(clause) + 1;
                }
            }
            else
            {
                id = 1;
            }
            string startCharUpper = StartUpperChar(objName);
            numberAuto = startCharUpper + DateTime.Today.Day.ToString() + DateTime.Today.Month.ToString() + DateTime.Today.Year.ToString() + id.ToString();
            return numberAuto;
        }

        /// <summary>
        /// Starts the upper char.
        /// </summary>
        /// <param name="objName">Name of the obj.</param>
        /// <returns></returns>
        public static string StartUpperChar(string objName)
        {
            string charResult = string.Empty;
            for (int i = 0; i < objName.Length; i++)
            {
                if (objName[i].ToString() == objName[i].ToString().ToUpper())
                {
                    charResult += objName[i].ToString();
                }
            }
            return charResult;
        }

    }
}
